package cn.pzhu.edu.imp;

import java.sql.*;
import java.util.ArrayList;

import cn.pzhu.edu.entity.User;
import cn.pzhu.edu.inf.UserDAO;
import cn.pzhu.edu.util.JdbcUtil;

public class UserDAOImp implements UserDAO{

	@Override
	public boolean insert(User data) {
		 Connection connection=null;
		 PreparedStatement sta=null;
		 boolean flag= false;
		 try{
				 connection = JdbcUtil.getConnection();
				String sql = "insert into user values(?,?)";
				 sta = connection.prepareStatement(sql);
				 sta.setString(1, data.getName());
				 sta.setString(2, data.getPassword());	
				 int n =sta.executeUpdate();
				 if(n>0){
					 flag=true;
				 }
			}
		 
			catch(Exception e){
				e.printStackTrace();
			}finally{
				JdbcUtil.close(connection, sta);
			}
		return flag;
	}

	@Override
	public boolean delete(String key) {
		 Connection connection=null;
		 PreparedStatement sta=null;
		 boolean flag= false;
		 try{
				 connection = JdbcUtil.getConnection();
				String sql = "DELETE FROM user WHERE name =? ; ";
				 sta = connection.prepareStatement(sql);
				 sta.setString(1, key);
				 int n =sta.executeUpdate();
				 if(n>0){
					 flag=true;
				 }
			}
			catch(Exception e){
				e.printStackTrace();
			}finally{
				JdbcUtil.close(connection, sta);
			}
		return flag;
}

	@Override
	public boolean update(User data) {
		Connection connection=null;
		 PreparedStatement sta=null;
		 boolean flag= false;
		 try{
				 connection = JdbcUtil.getConnection();
				String sql = "update user set password=? where name=?";
				 sta = connection.prepareStatement(sql);
				 sta.setString(1, data.getPassword());
				 sta.setString(2, data.getName());	
				 int n =sta.executeUpdate();
				 if(n>0){
					 flag=true;
				 }
			} 
			catch(Exception e){
				e.printStackTrace();
			}finally{
				JdbcUtil.close(connection, sta);
			}
		return flag;
	}

	@Override
	public User selectById(String key) {
		Connection connection = JdbcUtil.getConnection();
		String sql = "select * from user where name=?";
		PreparedStatement sta = null;
		ResultSet res = null;
		User user = null;
		try {
			sta = connection.prepareStatement(sql);
			sta.setString(1, key);
			res = sta.executeQuery();
			while(res.next()){
				user = new User();
				user.setName(res.getString(1));
				user.setPassword(res.getString(2));				
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JdbcUtil.close(res, sta, connection);
		}		
		return user;
	}


	@Override
	public ArrayList<User> selectAll() {
		Connection connection=null;
		 Statement sta=null;
		 ResultSet res=null;
		 try{
			    ArrayList<User> list = new ArrayList<>();
				 connection = JdbcUtil.getConnection();
				String sql = "select * from user";
				 sta = connection.createStatement();
				 res = sta.executeQuery(sql);
				 
				while(res.next()){
					User user = new User(res.getString(1),res.getString(2));
					list.add(user);
				}
				return list;
			}
		 
			catch(Exception e){
				e.printStackTrace();
			}finally{
				JdbcUtil.close(connection, sta, res);
			}
		return null;
	}

}
